﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace pdftron.PDF.Tools.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public sealed partial class ThumbnailViewer : UserControl, pdftron.PDF.Tools.Controls.ControlBase.ICloseableControl
    {
        private ViewModels.ThumbnailsViewViewModel _ViewModel;

        private PDFViewCtrl _PDFViewCtrl;
        public PDFViewCtrl PDFViewCtrl
        {
            private get { return _PDFViewCtrl; }
            set
            {
                _PDFViewCtrl = value;
                SetPDFViewCtrl(_PDFViewCtrl);
            }
        }

        public Color BlankPageDefaultColor
        {
            get
            {
                return _ViewModel.PageDefaultColor;
            }
            set
            {
                _ViewModel.PageDefaultColor = value;
            }
        }

        public ThumbnailViewer()
        {
            this.InitializeComponent();
            Init();
        }

        public ThumbnailViewer(PDFViewCtrl ctrl)
        {
            this.InitializeComponent();
            PDFViewCtrl = ctrl;
            Init();
        }

        public ThumbnailViewer(PDFViewCtrl ctrl, Color background)
        {

        }

        private void Init()
        {
            this.SizeChanged += ThumbnailViewer_SizeChanged;
            this.Unloaded += ThumbnailViewer_Unloaded;
        }

        void ThumbnailViewer_Unloaded(object sender, RoutedEventArgs e)
        {
            _ViewModel.CleanUp();
        }

        void ThumbnailViewer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width < 600)
            {
                MainGridView.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                MainGridView.DataContext = null;
                MainListView.Visibility = Windows.UI.Xaml.Visibility.Visible;
                MainListView.DataContext = _ViewModel;
                _ViewModel.CurrentListViewBase = MainListView;
            }
            else
            {
                MainGridView.Visibility = Windows.UI.Xaml.Visibility.Visible;
                MainGridView.DataContext = _ViewModel;
                MainListView.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                MainListView.DataContext = null;
                _ViewModel.CurrentListViewBase = MainGridView;
            }
        }

        private void SetPDFViewCtrl(PDFViewCtrl ctrl)
        {
            _ViewModel = new ViewModels.ThumbnailsViewViewModel(ctrl, this);
            this.DataContext = _ViewModel;
            _ViewModel.CurrentListViewBase = MainGridView;
        }

        public event pdftron.PDF.Tools.Controls.ControlBase.ControlClosedDelegate ControlClosed = delegate { };

        public void CloseControl()
        {
            ControlClosed();
        }
    }
}
